package com.example.controller;


import com.example.Comparator.OrderComparator;
import com.example.Comparator.OrderViewComparator;
import com.example.POJO.Order;
import com.example.POJO.OrderView;
import com.example.POJO.Product;
import com.example.service.OrderService;
import com.example.service.ProductService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/Order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;
    @PostMapping(value = "/add", consumes = "application/json;charset=UTF-8")
    public String add(@RequestBody HashMap<String, String> data) {
        try {
            Integer userId=Integer.valueOf(data.get("user_id"));
            Integer productId=Integer.valueOf(data.get("product_id"));
            Integer status=Integer.valueOf(data.get("status"));
            Integer count=Integer.valueOf(data.get("count"));
            Order order=new Order();
            order.setUser_id(userId);
            order.setProduct_id(productId);
            order.setCount(count);
            order.setStatus(status);
            // 获取当前时间
            LocalDateTime currentTime = LocalDateTime.now();

            // 定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            // 将当前时间格式化为字符串
            String time = currentTime.format(formatter);
            order.setTime(time);
            System.out.println(order);
            String res=orderService.addShoppingCast(order);
            return res;

        } catch (Exception e) {
            // 处理异常，返回相应的错误响应
            System.out.println(e);
            return "false";
        }
    }
    @PostMapping(value = "/view", consumes = "application/json;charset=UTF-8")
    public String getOrder(@RequestBody HashMap<String, String> data) throws IOException {
        Integer user_id=Integer.valueOf(data.get("user_id"));
        List<OrderView> list=new ArrayList<OrderView>();
        List<Order> orders=orderService.getOrderByUserId(user_id);
        for(int i=0;i<orders.size();i++) {
            OrderView orderView = new OrderView();
            Product product = productService.getProductByID(orders.get(i).getProduct_id());
            orderView.setCount(orders.get(i).getCount());
            orderView.setId(orders.get(i).getId());
            orderView.setStatus(orders.get(i).getStatus());
            orderView.setTime(orders.get(i).getTime());
            orderView.setUser_id(user_id);
            orderView.setName(product.getName());
            orderView.setPhoto(product.getPhoto());
            orderView.setPrice(product.getPrice());
            list.add(orderView);
        }
//        Collections.sort(list,new OrderViewComparator());
        // 对 list 进行逆序排序
        Collections.sort(list, Collections.reverseOrder(new OrderViewComparator()));
        Gson gson = new Gson();
        String res = gson.toJson(list);
        return res;
    }
    @PostMapping(value = "/submit", consumes = "application/json;charset=UTF-8")
    public void submit(@RequestBody HashMap<String, String> data) throws IOException {
        try{
            Integer order_id=Integer.valueOf(data.get("id"));
            orderService.setSubmitted(order_id);
        }catch (Exception e){
            System.out.println(e);
        }
    }
    @PostMapping(value = "/getSubmitOrder", consumes = "application/json;charset=UTF-8")
    public String getSubmittedOrder(@RequestBody HashMap<String, String> data) throws IOException {
        try{
            System.out.println(data);
            Integer user_id=Integer.valueOf(data.get("user_id"));
            System.out.println(user_id);
            List<OrderView> list=new ArrayList<OrderView>();
            List<Order> orders=orderService.getSubmittedOrderByUserId(user_id);
            for(int i=0;i<orders.size();i++) {
                OrderView orderView = new OrderView();
                Product product = productService.getProductByID(orders.get(i).getProduct_id());
                orderView.setCount(orders.get(i).getCount());
                orderView.setId(orders.get(i).getId());
                orderView.setStatus(orders.get(i).getStatus());
                orderView.setTime(orders.get(i).getTime());
                orderView.setUser_id(user_id);
                orderView.setName(product.getName());
                orderView.setPhoto(product.getPhoto());
                orderView.setPrice(product.getPrice());
                list.add(orderView);
            }
//            Collections.sort(list,new OrderViewComparator());
            // 对 list 进行逆序排序
            Collections.sort(list, Collections.reverseOrder(new OrderViewComparator()));
            Gson gson = new Gson();
            String res = gson.toJson(list);
            return res;
        }catch (Exception e){
            System.out.println(e);
            return "";
        }
    }

}
